{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Panda怎样新增数据列？\n",
    "在进行数据分析时，经常需要按照一定条件创建新的数据列，然后进行进一步分析。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. 直接赋值\n",
    "2. df.apply方法\n",
    "3. df.assign方法\n",
    "4. 按条件选择后分别赋值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 0、读取csv数据到dataframe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "fpath = \"./datas/beijing_tianqi/beijing_tianqi_2018.csv\"\n",
    "df = pd.read_csv(fpath)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ymd</th>\n",
       "      <th>bWendu</th>\n",
       "      <th>yWendu</th>\n",
       "      <th>tianqi</th>\n",
       "      <th>fengxiang</th>\n",
       "      <th>fengli</th>\n",
       "      <th>aqi</th>\n",
       "      <th>aqiInfo</th>\n",
       "      <th>aqiLevel</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>2018-01-01</td>\n",
       "      <td>3℃</td>\n",
       "      <td>-6℃</td>\n",
       "      <td>晴~多云</td>\n",
       "      <td>东北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>59</td>\n",
       "      <td>良</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>2018-01-02</td>\n",
       "      <td>2℃</td>\n",
       "      <td>-5℃</td>\n",
       "      <td>阴~多云</td>\n",
       "      <td>东北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>49</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>2018-01-03</td>\n",
       "      <td>2℃</td>\n",
       "      <td>-5℃</td>\n",
       "      <td>多云</td>\n",
       "      <td>北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>28</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>2018-01-04</td>\n",
       "      <td>0℃</td>\n",
       "      <td>-8℃</td>\n",
       "      <td>阴</td>\n",
       "      <td>东北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>28</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>2018-01-05</td>\n",
       "      <td>3℃</td>\n",
       "      <td>-6℃</td>\n",
       "      <td>多云~晴</td>\n",
       "      <td>西北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>50</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          ymd bWendu yWendu tianqi fengxiang fengli  aqi aqiInfo  aqiLevel\n",
       "0  2018-01-01     3℃    -6℃   晴~多云       东北风   1-2级   59       良         2\n",
       "1  2018-01-02     2℃    -5℃   阴~多云       东北风   1-2级   49       优         1\n",
       "2  2018-01-03     2℃    -5℃     多云        北风   1-2级   28       优         1\n",
       "3  2018-01-04     0℃    -8℃      阴       东北风   1-2级   28       优         1\n",
       "4  2018-01-05     3℃    -6℃   多云~晴       西北风   1-2级   50       优         1"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 替换掉温度的后缀℃\n",
    "df.loc[:, \"bWendu\"] = df[\"bWendu\"].str.replace(\"℃\", \"\").astype('int32')\n",
    "df.loc[:, \"yWendu\"] = df[\"yWendu\"].str.replace(\"℃\", \"\").astype('int32')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1、直接赋值的方法  \n",
    "实例：计算温差"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 注意，df[\"bWendu\"]其实是一个Series，后面的减法返回的是Series\n",
    "df.loc[:, \"wencha\"] = df[\"bWendu\"] - df[\"yWendu\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ymd</th>\n",
       "      <th>bWendu</th>\n",
       "      <th>yWendu</th>\n",
       "      <th>tianqi</th>\n",
       "      <th>fengxiang</th>\n",
       "      <th>fengli</th>\n",
       "      <th>aqi</th>\n",
       "      <th>aqiInfo</th>\n",
       "      <th>aqiLevel</th>\n",
       "      <th>wencha</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>2018-01-01</td>\n",
       "      <td>3</td>\n",
       "      <td>-6</td>\n",
       "      <td>晴~多云</td>\n",
       "      <td>东北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>59</td>\n",
       "      <td>良</td>\n",
       "      <td>2</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>2018-01-02</td>\n",
       "      <td>2</td>\n",
       "      <td>-5</td>\n",
       "      <td>阴~多云</td>\n",
       "      <td>东北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>49</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>2018-01-03</td>\n",
       "      <td>2</td>\n",
       "      <td>-5</td>\n",
       "      <td>多云</td>\n",
       "      <td>北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>28</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>2018-01-04</td>\n",
       "      <td>0</td>\n",
       "      <td>-8</td>\n",
       "      <td>阴</td>\n",
       "      <td>东北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>28</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>2018-01-05</td>\n",
       "      <td>3</td>\n",
       "      <td>-6</td>\n",
       "      <td>多云~晴</td>\n",
       "      <td>西北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>50</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          ymd  bWendu  yWendu tianqi fengxiang fengli  aqi aqiInfo  aqiLevel  \\\n",
       "0  2018-01-01       3      -6   晴~多云       东北风   1-2级   59       良         2   \n",
       "1  2018-01-02       2      -5   阴~多云       东北风   1-2级   49       优         1   \n",
       "2  2018-01-03       2      -5     多云        北风   1-2级   28       优         1   \n",
       "3  2018-01-04       0      -8      阴       东北风   1-2级   28       优         1   \n",
       "4  2018-01-05       3      -6   多云~晴       西北风   1-2级   50       优         1   \n",
       "\n",
       "   wencha  \n",
       "0       9  \n",
       "1       7  \n",
       "2       7  \n",
       "3       8  \n",
       "4       9  "
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2、df.apply方法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Apply a function along an axis of the DataFrame.\n",
    "\n",
    "Objects passed to the function are Series objects whose index is either the DataFrame’s index (axis=0) or the DataFrame’s columns (axis=1). \n",
    "\n",
    "实例：添加一列是否高温，如果最高温度大于33度就是高温，低于-10度是低温，中间是常温"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_wendu_type(df):\n",
    "    if df[\"bWendu\"] > 33:\n",
    "        return '高温'\n",
    "    if df[\"yWendu\"] < -10:\n",
    "        return '低温'\n",
    "    return '常温'\n",
    "\n",
    "# 注意需要设置axis==1，这是series的index是columns\n",
    "df.loc[:, \"wendu_type\"] = df.apply(get_wendu_type, axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "常温    328\n",
       "高温     29\n",
       "低温      8\n",
       "Name: wendu_type, dtype: int64"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看温度类型的计数\n",
    "df[\"wendu_type\"].value_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3、df.assign方法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Assign new columns to a DataFrame.\n",
    "\n",
    "Returns a new object with all original columns in addition to new ones. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ymd</th>\n",
       "      <th>bWendu</th>\n",
       "      <th>yWendu</th>\n",
       "      <th>tianqi</th>\n",
       "      <th>fengxiang</th>\n",
       "      <th>fengli</th>\n",
       "      <th>aqi</th>\n",
       "      <th>aqiInfo</th>\n",
       "      <th>aqiLevel</th>\n",
       "      <th>wencha</th>\n",
       "      <th>wendu_type</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>2018-01-01</td>\n",
       "      <td>3</td>\n",
       "      <td>-6</td>\n",
       "      <td>晴~多云</td>\n",
       "      <td>东北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>59</td>\n",
       "      <td>良</td>\n",
       "      <td>2</td>\n",
       "      <td>9</td>\n",
       "      <td>常温</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>2018-01-02</td>\n",
       "      <td>2</td>\n",
       "      <td>-5</td>\n",
       "      <td>阴~多云</td>\n",
       "      <td>东北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>49</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "      <td>7</td>\n",
       "      <td>常温</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>2018-01-03</td>\n",
       "      <td>2</td>\n",
       "      <td>-5</td>\n",
       "      <td>多云</td>\n",
       "      <td>北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>28</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "      <td>7</td>\n",
       "      <td>常温</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>2018-01-04</td>\n",
       "      <td>0</td>\n",
       "      <td>-8</td>\n",
       "      <td>阴</td>\n",
       "      <td>东北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>28</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "      <td>8</td>\n",
       "      <td>常温</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>2018-01-05</td>\n",
       "      <td>3</td>\n",
       "      <td>-6</td>\n",
       "      <td>多云~晴</td>\n",
       "      <td>西北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>50</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "      <td>9</td>\n",
       "      <td>常温</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          ymd  bWendu  yWendu tianqi fengxiang fengli  aqi aqiInfo  aqiLevel  \\\n",
       "0  2018-01-01       3      -6   晴~多云       东北风   1-2级   59       良         2   \n",
       "1  2018-01-02       2      -5   阴~多云       东北风   1-2级   49       优         1   \n",
       "2  2018-01-03       2      -5     多云        北风   1-2级   28       优         1   \n",
       "3  2018-01-04       0      -8      阴       东北风   1-2级   28       优         1   \n",
       "4  2018-01-05       3      -6   多云~晴       西北风   1-2级   50       优         1   \n",
       "\n",
       "   wencha wendu_type  \n",
       "0       9         常温  \n",
       "1       7         常温  \n",
       "2       7         常温  \n",
       "3       8         常温  \n",
       "4       9         常温  "
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ymd</th>\n",
       "      <th>bWendu</th>\n",
       "      <th>yWendu</th>\n",
       "      <th>tianqi</th>\n",
       "      <th>fengxiang</th>\n",
       "      <th>fengli</th>\n",
       "      <th>aqi</th>\n",
       "      <th>aqiInfo</th>\n",
       "      <th>aqiLevel</th>\n",
       "      <th>wencha</th>\n",
       "      <th>wendu_type</th>\n",
       "      <th>yWendu_huashi</th>\n",
       "      <th>bWendu_huashi</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>2018-01-01</td>\n",
       "      <td>3</td>\n",
       "      <td>-6</td>\n",
       "      <td>晴~多云</td>\n",
       "      <td>东北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>59</td>\n",
       "      <td>良</td>\n",
       "      <td>2</td>\n",
       "      <td>9</td>\n",
       "      <td>常温</td>\n",
       "      <td>21.2</td>\n",
       "      <td>37.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>2018-01-02</td>\n",
       "      <td>2</td>\n",
       "      <td>-5</td>\n",
       "      <td>阴~多云</td>\n",
       "      <td>东北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>49</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "      <td>7</td>\n",
       "      <td>常温</td>\n",
       "      <td>23.0</td>\n",
       "      <td>35.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>2018-01-03</td>\n",
       "      <td>2</td>\n",
       "      <td>-5</td>\n",
       "      <td>多云</td>\n",
       "      <td>北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>28</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "      <td>7</td>\n",
       "      <td>常温</td>\n",
       "      <td>23.0</td>\n",
       "      <td>35.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>2018-01-04</td>\n",
       "      <td>0</td>\n",
       "      <td>-8</td>\n",
       "      <td>阴</td>\n",
       "      <td>东北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>28</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "      <td>8</td>\n",
       "      <td>常温</td>\n",
       "      <td>17.6</td>\n",
       "      <td>32.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>2018-01-05</td>\n",
       "      <td>3</td>\n",
       "      <td>-6</td>\n",
       "      <td>多云~晴</td>\n",
       "      <td>西北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>50</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "      <td>9</td>\n",
       "      <td>常温</td>\n",
       "      <td>21.2</td>\n",
       "      <td>37.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>360</td>\n",
       "      <td>2018-12-27</td>\n",
       "      <td>-5</td>\n",
       "      <td>-12</td>\n",
       "      <td>多云~晴</td>\n",
       "      <td>西北风</td>\n",
       "      <td>3级</td>\n",
       "      <td>48</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "      <td>7</td>\n",
       "      <td>低温</td>\n",
       "      <td>10.4</td>\n",
       "      <td>23.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>361</td>\n",
       "      <td>2018-12-28</td>\n",
       "      <td>-3</td>\n",
       "      <td>-11</td>\n",
       "      <td>晴</td>\n",
       "      <td>西北风</td>\n",
       "      <td>3级</td>\n",
       "      <td>40</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "      <td>8</td>\n",
       "      <td>低温</td>\n",
       "      <td>12.2</td>\n",
       "      <td>26.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>362</td>\n",
       "      <td>2018-12-29</td>\n",
       "      <td>-3</td>\n",
       "      <td>-12</td>\n",
       "      <td>晴</td>\n",
       "      <td>西北风</td>\n",
       "      <td>2级</td>\n",
       "      <td>29</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "      <td>9</td>\n",
       "      <td>低温</td>\n",
       "      <td>10.4</td>\n",
       "      <td>26.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>363</td>\n",
       "      <td>2018-12-30</td>\n",
       "      <td>-2</td>\n",
       "      <td>-11</td>\n",
       "      <td>晴~多云</td>\n",
       "      <td>东北风</td>\n",
       "      <td>1级</td>\n",
       "      <td>31</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "      <td>9</td>\n",
       "      <td>低温</td>\n",
       "      <td>12.2</td>\n",
       "      <td>28.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>364</td>\n",
       "      <td>2018-12-31</td>\n",
       "      <td>-2</td>\n",
       "      <td>-10</td>\n",
       "      <td>多云</td>\n",
       "      <td>东北风</td>\n",
       "      <td>1级</td>\n",
       "      <td>56</td>\n",
       "      <td>良</td>\n",
       "      <td>2</td>\n",
       "      <td>8</td>\n",
       "      <td>常温</td>\n",
       "      <td>14.0</td>\n",
       "      <td>28.4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>365 rows × 13 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            ymd  bWendu  yWendu tianqi fengxiang fengli  aqi aqiInfo  \\\n",
       "0    2018-01-01       3      -6   晴~多云       东北风   1-2级   59       良   \n",
       "1    2018-01-02       2      -5   阴~多云       东北风   1-2级   49       优   \n",
       "2    2018-01-03       2      -5     多云        北风   1-2级   28       优   \n",
       "3    2018-01-04       0      -8      阴       东北风   1-2级   28       优   \n",
       "4    2018-01-05       3      -6   多云~晴       西北风   1-2级   50       优   \n",
       "..          ...     ...     ...    ...       ...    ...  ...     ...   \n",
       "360  2018-12-27      -5     -12   多云~晴       西北风     3级   48       优   \n",
       "361  2018-12-28      -3     -11      晴       西北风     3级   40       优   \n",
       "362  2018-12-29      -3     -12      晴       西北风     2级   29       优   \n",
       "363  2018-12-30      -2     -11   晴~多云       东北风     1级   31       优   \n",
       "364  2018-12-31      -2     -10     多云       东北风     1级   56       良   \n",
       "\n",
       "     aqiLevel  wencha wendu_type  yWendu_huashi  bWendu_huashi  \n",
       "0           2       9         常温           21.2           37.4  \n",
       "1           1       7         常温           23.0           35.6  \n",
       "2           1       7         常温           23.0           35.6  \n",
       "3           1       8         常温           17.6           32.0  \n",
       "4           1       9         常温           21.2           37.4  \n",
       "..        ...     ...        ...            ...            ...  \n",
       "360         1       7         低温           10.4           23.0  \n",
       "361         1       8         低温           12.2           26.6  \n",
       "362         1       9         低温           10.4           26.6  \n",
       "363         1       9         低温           12.2           28.4  \n",
       "364         2       8         常温           14.0           28.4  \n",
       "\n",
       "[365 rows x 13 columns]"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 可以同时添加多个新的列\n",
    "df.assign(\n",
    "    yWendu_huashi = lambda x : x[\"yWendu\"] * 9 / 5 + 32,\n",
    "    # 摄氏度转华氏度\n",
    "    bWendu_huashi = lambda x : x[\"bWendu\"] * 9 / 5 + 32\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4、按条件选择后分别赋值\n",
    "先选择一部分数据，然后对这部分数据赋值新列\n",
    "实例：高低温差大于10度，则认为温差大"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 先创建空列（这是第一种创建新列的方法）\n",
    "df['wencha_type'] = ''\n",
    "\n",
    "df.loc[df[\"bWendu\"]-df[\"yWendu\"]>10, \"wencha_type\"] = \"温差大\"\n",
    "\n",
    "df.loc[df[\"bWendu\"]-df[\"yWendu\"]<=10, \"wencha_type\"] = \"温差正常\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "温差正常    187\n",
       "温差大     178\n",
       "Name: wencha_type, dtype: int64"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\"wencha_type\"].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
